package com.hongxin.nw.netty.down;

import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_1;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @remark A.6黑名单下发时下行数据
 * @author: GuoXiaolin
 * @date 2019/6/21 17:40
 * @Version 1.0
 * @Descibe
 */
public class BlackListReq implements IChannelRequest {
    @Override
    public String doPacking(Transfer transfer) throws Exception {

        String code = transfer.getCode();
        List<Map<String ,Object>> list = new ArrayList<>();
        for (int i =0 ;i < 2;i++){
            Map<String ,Object> map = new HashMap<>();
            map.put("physicsNum","3212121"); //物理卡号	压缩BCD码	 8Byte  16位编码
            map.put("status",1); //状态	压缩BCD码 	1Byte	1-挂失 2-解挂
            list.add(map);
        }
        Csdna1_1 csdna1_1 = new Csdna1_1();
        csdna1_1 = csdna1_1.setCo(code)
                .setChargeIdentify("0")
                .setTimeNum(String.valueOf(System.currentTimeMillis()))
                .setBlackNum(String.valueOf(list.size()))
                .setPhysicsNums(list);

        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_BLACK_LIST.getInt()));
        mm.setSendData(csdna1_1.packing());


//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setVsq(list.size(), StaticValue.SQ0);
//        as.setCot(TypeIdentif.COT_UNKNOWN_COT.getInt());
//        as.setInfoadr(0);
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());

        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_UNKNOWN_COT.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(),0, list.size(), StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt())
//                .setApduLength(as.getHexData().length()/2)
//                .setContarea(TypeIdentif.FORMAT_I.getStr(),StaticValue.U_START_V);
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_START_V);
        return ac + data;
    }
}
